﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
using DomainModel;

namespace DataAccess
{
    public class ProductAccess : IProductProvider
    {
        #region Category Control

        public void CreateCategory(Category newCategory)
        {
            using (Main db = new Main())
            {
                db.GetTable<Category>().InsertOnSubmit(newCategory);
                db.SubmitChanges();
            }
        }

        public void DeleteCategoryByID(int id)
        {
            using (Main db = new Main())
            {
                var query = (from c in db.GetTable<Category>()
                             where c.ID == id
                             select c).First<Category>();

                db.GetTable<Category>().DeleteOnSubmit(query);
                db.SubmitChanges();
            }
        }

        public void UpdateCategory(Category category)
        {
            using (Main db = new Main())
            {
                var query = (from c in db.GetTable<Category>()
                             where c.ID == category.ID
                             select c).First<Category>();

                query.CategoryName = category.CategoryName;
                query.ParentID = category.ParentID;
                query.Rank = category.Rank;

                db.SubmitChanges();
            }
        }

        public IEnumerable<Category> GetAllCategories()
        {
            using (Main db = new Main())
            {
                return (from c in db.GetTable<Category>()
                        select c).AsEnumerable<Category>();
            }
        }

        public IEnumerable<Category> GetChildrenCategoriesByParentID(int parentId)
        {
            using (Main db = new Main())
            {
                return (from c in db.GetTable<Category>()
                        where c.ParentID == parentId
                        select c).AsEnumerable<Category>();
            }
        }

        public Category GetCategoryByID(int id)
        {
            using (Main db = new Main())
            {
                return (from c in db.GetTable<Category>()
                        where c.ID == id
                        select c).First<Category>();
            }
        }

        #endregion



        #region Product Control


        public void CreateProduct(Product product)
        {
            using (Main db = new Main())
            {
                db.GetTable<Product>().InsertOnSubmit(product);
                db.SubmitChanges();
            }
        }

        public void DeleteProductByID(int id)
        {
            using (Main db = new Main())
            {
                var query = (from p in db.GetTable<Product>()
                             where p.ID == id
                             select p).Single<Product>();

                db.GetTable<Product>().DeleteOnSubmit(query);
                db.SubmitChanges();
            }
        }

        public void UpdateProduct(Product product)
        {
            using (Main db = new Main())
            {
                var query = (from p in db.GetTable<Product>()
                             where p.ID == product.ID
                             select p).Single<Product>();

                query.AccessCount = product.AccessCount;
                query.CategoryID = product.CategoryID;
                query.Detail = product.Detail;
                query.ImageUrl = product.ImageUrl;
                query.Model = product.Model;
                query.Price = product.Price;
                query.ProductName = product.ProductName;
                query.Rank = product.Rank;
                query.Recommend = product.Recommend;

                db.SubmitChanges();
            }
        }

        public IEnumerable<Product> GetAllProducts()
        {
            using (Main db = new Main())
            {
                return (from p in db.GetTable<Product>()
                        select p).AsEnumerable<Product>();
            }
        }

        public Product GetProductByID(int id)
        {
            using (Main db = new Main())
            {
                return (from p in db.GetTable<Product>()
                        where p.ID == id
                        select p).Single<Product>();
            }
        }

        #endregion

        #region Order Control


        public IEnumerable<Order> GetAllOrders()
        {
            using (Main db = new Main())
            {
                return (from o in db.GetTable<Order>()
                        select o).AsEnumerable<Order>();
            }
        }

        public void DeleteOrderByID(int id)
        {
            using (Main db = new Main())
            {
                var query = (from o in db.GetTable<Order>()
                             where o.ID == id
                             select o).First<Order>();
                db.GetTable<Order>().DeleteOnSubmit(query);
                db.SubmitChanges();
            }
        }

        public Order GetOrderByID(int id)
        {
            using (Main db = new Main())
            {
                return (from o in db.GetTable<Order>()
                        where o.ID == id
                        select o).Single<Order>();
            }
        }

        public IEnumerable<OrderDetail> GetOrderDetailsByOrderID(int id)
        {
            using (Main db = new Main())
            {
                return (from o in db.GetTable<OrderDetail>()
                        where o.OrderID == id
                        select o).AsEnumerable<OrderDetail>();
            }
        }

        #endregion
    }
}
